<?php

namespace addons\yunfood\controller;

use addons\yunfood\model\Shop;
use app\common\controller\Api;

/**
 * Api 基础
 */
class BaseApi extends Api
{
    protected $noNeedRight = ['*'];

    protected $userId;
    protected $userData;
    protected $shopId;

    /**
     * 初始化操作
     * @access protected
     */
    protected function _initialize()
    {
        parent::_initialize();

        // 跨域请求检测
        check_cors_request();

        if ($this->auth->isLogin()) {
            $this->userId = $this->auth->id;
            $this->userData = $this->auth->getUser();
        }
        $this->shopId = $this->request->get('shop_id');
    }

    protected function validShop($validTime = true)
    {
        if (! $this->shopId) {
            $this->error('shop_id不能为空');
        }
        if (! $shopData = Shop::get($this->shopId)) {
            $this->error('门店数据不存在');
        }
        if ($shopData->status != Shop::STATUS_NORMAL) {
            $this->error('当前店还未营业');
        }
        if ($validTime) {
            if ($shopData->state != Shop::STATE_ING) {
                $this->error('当前店已休息');
            }
        }

        return $shopData;
    }
}
